home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Interactive Web Graphics with Shout 3D
/
Interactive Web Graphics With Shout 3D.iso
/
pc
/
Code
/
Chapter Code.exe
/
Chapter08
/
RotateMovePanel.java
< prev
next >
Wrap
Text File
|
2000-07-01
|
2KB
|
129 lines
package applets;
import shout3d.*;
import shout3d.core.*;
import shout3d.math.*;
public class RotateMovePanel extends Shout3DPanel implements DeviceObserver{
Transform mover;
int pixelStartX;
int pixelStartY;
int pixelEndX;
int pixelEndY;
float[] worldPos = new float[3];
float [] eulers = new float [3];
float [] axisAngle = new float [4];
Quaternion q = new Quaternion();
public RotateMovePanel (Shout3DApplet applet){
super(applet);
}
public void customInitialize() {
addDeviceObserver(this,"MouseInput", null);
mover = (Transform) getNodeByName("vehicle");
worldPos = mover.translation.getValue();
axisAngle = mover.rotation.getValue();
q.setAxisAngle(axisAngle);
q.getEulers(eulers);
}
protected void finalize() {
removeDeviceObserver(this,"MouseInput");
}
public boolean onDeviceInput(DeviceInput di, Object userData) {
MouseInput mi = (MouseInput) di;
switch (mi.which){
case MouseInput.DOWN:
pixelStartX = mi.x;
pixelStartY = mi.y;
return true;
case MouseInput.DRAG:
//PIXEL OPERATIONS
pixelEndX = mi.x;
pixelEndY = mi.y;
int dragDistanceX = pixelEndX - pixelStartX;
int dragDistanceY = pixelEndY - pixelStartY;
pixelStartX = pixelEndX;
pixelStartY = pixelEndY;
//ROTATING THE OBJECT
//convert x drag to rotation
// at 30 pixels/radian
float rotationDelta = -(dragDistanceX/30f);
//compute new heading
eulers[0] = eulers[0] + rotationDelta;
//Convert from Eulers to AxisAngle
// using the Quaternion
q.setEulers(eulers);
q.getAxisAngle(axisAngle);
//set the Transform to
//updated axis angle values
mover.rotation.setValue(axisAngle);
//TRANSLATING THE OBJECT
//covert y drag to distance
//at 2 meters per pixel
float translationDelta = dragDistanceY * 2.0f;
//make a vector pointing
//in z direction
float[] vector = {0,0, translationDelta};
//rotate the vector using
//value in quaternion
q.xform(vector);
//add rotated vector
//to current position
worldPos[0] = worldPos[0] + vector[0];
worldPos[2] = worldPos[2] + vector[2];
//set Transform to new position
mover.translation.setValue(worldPos);
return true;
}//end of switch
return false;
}
} //end of class